
Imports System
Imports System.Collections
Imports System.Data.SqlClient
Imports System.Diagnostics
Imports System.Drawing
Imports System.IO
Imports _2bMatesSite.Common

Namespace _2bMatesSite.BO
	''' <summary>
	''' Summary description for GroupEvent.
	''' </summary>
	Public Class GroupEvent
		'
		' Members
		'

		Protected id As Integer
		Protected groupID As Integer
		Protected dateTimeEvent As DateTime
		Protected venue As String
		Protected cost As String
		Protected address As String
		Protected city As String
		Protected zipCode As String
		Protected countryID As Integer
		Protected details As String

		Public Sub New(ByVal grpID As Integer)
			Init(grpID)
		End Sub

		' Loads GroupEvent from a data reader
		Public Sub New(ByVal dr As SqlDataReader)
			LoadFromReader(dr)
		End Sub

		'
		' Properties
		'

		Public ReadOnly Property ID() As Integer
			Get
				Return id
			End Get
		End Property

		Public Property GroupID() As Integer
			Get
				Return groupID
			End Get

			Set
				groupID = value
			End Set
		End Property

		Public Property DateTimeEvent() As DateTime
			Get
				Return dateTimeEvent
			End Get

			Set
				dateTimeEvent = value
			End Set
		End Property

		Public Property Venue() As String
			Get
				Return venue
			End Get

			Set
				venue = value
			End Set
		End Property

		Public Property Cost() As String
			Get
				Return cost
			End Get

			Set
				cost = value
			End Set
		End Property

		Public Property Address() As String
			Get
				Return address
			End Get

			Set
				address = value
			End Set
		End Property

		Public Property City() As String
			Get
				Return city
			End Get

			Set
				city = value
			End Set
		End Property

		Public Property ZipCode() As String
			Get
				Return zipCode
			End Get

			Set
				zipCode = value
			End Set
		End Property

		Public Property CountryID() As Integer
			Get
				Return countryID
			End Get

			Set
				countryID = value
			End Set
		End Property

		Public Property Details() As String
			Get
				Return details
			End Get

			Set
				details = value
			End Set
		End Property


		'
		' Public methods
		'


		' Save the modifications to the groupEvent made so far
		' If it's a new groupEvent inserts it into the database
		Public Sub Save()
			If id < 0 Then
				DoInsert()
			Else
				DoUpdate()
			End If
		End Sub


		'
		' Private and protected methods
		'

		Protected Sub Init(ByVal grpID As Integer)
			id = -1
			groupID = grpID
			dateTimeEvent = DateTime.Now
			venue = ""
			cost = ""
			address = ""
			city = ""
			zipCode = ""
			countryID = -1
			details = ""
		End Sub

		Protected Sub LoadFromReader(ByVal dr As SqlDataReader)
			id = dr.GetInt32(dr.GetOrdinal("ID"))

			groupID = dr.GetInt32(dr.GetOrdinal("GroupID"))

			dateTimeEvent = dr.GetDateTime(dr.GetOrdinal("DateTime"))

			venue = DBManager.GetNZString(dr, "Venue")

			cost = DBManager.GetNZString(dr, "Cost")

			address = DBManager.GetNZString(dr, "Address")

			city = DBManager.GetNZString(dr, "City")

			zipCode = DBManager.GetNZString(dr, "ZipCode")

			countryID = dr.GetInt32(dr.GetOrdinal("CountryID"))

			details = DBManager.GetNZString(dr, "Details")

		End Sub

		Protected Sub DoInsert()
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Dim tran As SqlTransaction = conn.BeginTransaction()

			Try
				' Insert the groupEvent
				Dim cmd As New SqlCommand(GetInsertSQL())
				cmd.Connection = conn
				cmd.Transaction = tran

				PopulateCommand(cmd)

				Dim rows As Integer = cmd.ExecuteNonQuery()
				Debug.Assert(rows = 1)

				tran.Commit()
			Catch e As Exception
				tran.Rollback()
				Throw e
			Finally
				conn.Close()
			End Try
		End Sub

		Protected Sub DoUpdate()
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Dim tran As SqlTransaction = conn.BeginTransaction()

			Try
				Dim cmd As New SqlCommand(GetUpdateSQL())
				cmd.Connection = conn
				cmd.Transaction = tran

				PopulateCommand(cmd)

				cmd.Parameters.Add("@ID", id)

				Dim rows As Integer = cmd.ExecuteNonQuery()
				Debug.Assert(rows = 1)

				tran.Commit()
			Catch e As Exception
				tran.Rollback()
				Throw e
			Finally
				conn.Close()
			End Try
		End Sub

		Public Sub DoDelete()
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()

			Try
				' Delete the groupEvent
				Dim cmd As New SqlCommand(GetDeleteSQL())
				cmd.Connection = conn

				cmd.Parameters.Add("@ID", id)

				Dim rows As Integer = cmd.ExecuteNonQuery()

				Debug.Assert(rows = 1)
			Finally
				conn.Close()
			End Try
		End Sub

		Private Shared Function GetDeleteSQL() As String
			Dim sql As String = "DELETE FROM GroupEvents" + Environment.NewLine
			sql += "WHERE" + Environment.NewLine
			sql += "" & Chr(9) & "ID=@ID" + Environment.NewLine
			Return sql
		End Function

		Private Shared Function GetInsertSQL() As String
			Dim sql As String = "INSERT INTO GroupEvents" + Environment.NewLine
			sql += "( GroupID, DateTime, Venue, Cost, Address, City, ZipCode, CountryID, Details )" + Environment.NewLine
			sql += "VALUES" + Environment.NewLine
			sql += "( @GroupID, @DateTime, @Venue, @Cost, @Address, @City, @ZipCode, @CountryID, @Details )" + Environment.NewLine
			Return sql
		End Function

		Private Shared Function GetUpdateSQL() As String
			Dim sql As String = "UPDATE GroupEvents SET" + Environment.NewLine
			sql += "" & Chr(9) & "GroupID=@GroupID," + Environment.NewLine
			sql += "" & Chr(9) & "DateTime=@DateTime," + Environment.NewLine
			sql += "" & Chr(9) & "Venue=@Venue," + Environment.NewLine
			sql += "" & Chr(9) & "Cost=@Cost," + Environment.NewLine
			sql += "" & Chr(9) & "Address=@Address," + Environment.NewLine
			sql += "" & Chr(9) & "City=@City," + Environment.NewLine
			sql += "" & Chr(9) & "ZipCode=@ZipCode," + Environment.NewLine
			sql += "" & Chr(9) & "CountryID=@CountryID," + Environment.NewLine
			sql += "" & Chr(9) & "Details=@Details" + Environment.NewLine
			sql += "WHERE" + Environment.NewLine
			sql += "" & Chr(9) & "ID=@ID" + Environment.NewLine
			Return sql
		End Function

		' Populates the command with the groups's properties
		' NOTE: skips the ID property, since it's need only on insert
		Private Sub PopulateCommand(ByVal cmd As SqlCommand)
			cmd.Parameters.Add("@GroupID", groupID)
			cmd.Parameters.Add("@DateTime", dateTimeEvent)
			DBManager.SetNZValue(cmd, "@Venue", venue)
			DBManager.SetNZValue(cmd, "@Cost", cost)
			DBManager.SetNZValue(cmd, "@Address", address)
			DBManager.SetNZValue(cmd, "@City", city)
			DBManager.SetNZValue(cmd, "@ZipCode", zipCode)
			cmd.Parameters.Add("@CountryID", countryID)
			DBManager.SetNZValue(cmd, "@Details", details)
		End Sub
	End Class
End Namespace






